package com.cc.servlet;

import com.alibaba.fastjson.JSON;
import com.cc.entity.AttendanceRecord;
import com.cc.service.AttendanceService;
import com.cc.service.impl.AttendanceServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 86130
 * @version 1.0
 * @description: TODO
 * @date 2025/3/31 09:30
 */
@WebServlet("/attendance")
public class AttendanceServlet extends HttpServlet {
    AttendanceService attendanceService = new AttendanceServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/json;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        switch (method){
            case "clockIn":
                clockIn(req, resp);
                break;
            case "list":
                list(req, resp);
                break;
        }
    }
    // 打卡记录
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String employeeId = req.getParameter("employeeId");
        String startDate = req.getParameter("startDate");
        String endDate = req.getParameter("endDate");
        List<AttendanceRecord> list = attendanceService.getAttendanceList(employeeId,startDate,endDate);
        String json = JSON.toJSONString(list);
        resp.getWriter().write(json);

    }

    // 打卡
    private void clockIn(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/json;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        String employeeId = req.getParameter("employeeId");
        attendanceService.clockIn(employeeId);
        Map<String, Object> map = new HashMap<>();
        map.put("code","200");
        map.put("msg","打卡成功");
        String mapJSON = JSON.toJSONString(map);
        resp.getWriter().write(mapJSON);
    }
}
